$(document).ready(function (){
    var $SearchTextField=$('.SearchTextField');
    var $SearchAutoCompleteDiv=$('.SearchAutoCompleteDiv');
    var $SearchAutoComplete=$('.SearchAutoComplete');

    $(document).on('keyup','.SearchTextField',function (){
        var keyword=$(this).val();
        console.log(keyword);
        if(keyword!=''&&keyword!=null) {
            $.ajax({
                type: 'GET',
                url: 'http://localhost:8080/my_pet_store_war_exploded/searchAutoComplete?keyword=' + keyword,

                success: function (data) {
                    console.log(data);
                    var productListHtml = '';
                    for (var i = 0; i < data.length; i++) {
                        productListHtml += '<li data-productId="';
                        productListHtml += data[i].productId;
                        productListHtml += '">';
                        productListHtml += i+1;
                        if(i+1<10) {
                            productListHtml += '\xa0'+'\xa0'+'\xa0';
                        }else{
                            productListHtml += '\xa0';
                        }
                        productListHtml += data[i].categoryId;
                        productListHtml += ': ';
                        productListHtml += data[i].name;
                        productListHtml += '</li>';
                    }

                    $SearchAutoCompleteDiv.show();
                    $SearchAutoComplete.html(productListHtml)

                },
                error: function (errorMessage) {
                    console.log(errorMessage);
                },

            });
        }else{
            $SearchAutoComplete.html('');
        }


    });

    $('.Search').on('mouseleave',function (){
        $SearchAutoCompleteDiv.hide();
    })

    $('.Search').on('mouseenter',function (){
        $SearchAutoCompleteDiv.show();
    })

    $(document).on('click','li',function(){
        var productId=($(this).data('productid'));
        window.location.href="http://localhost:8080/my_pet_store_war_exploded/productForm?productId="+productId;

    })


});
